//
//  MemberListViewController.m
//  League
//
//  Created by JinHvung on 2018/2/13.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#define MemberListCellH 90
#import "MemberListViewController.h"
#import "LoginModel.h"
#import "MemberTableViewCell.h"
#import "OthersProfileViewController.h"

@interface MemberListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray<LoginModel *> *models;
@property (nonatomic, assign)NSInteger pageIndex;


@end

static NSString *MemberTableViewCellID = @"MemberTableViewCellID";
@implementation MemberListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.models = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    [self.tableView.mj_header beginRefreshing];
}


- (UITableView *)tableView {
    if(!_tableView) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, CONTENT_HEIGHT);
        tableView.delegate = self;
        tableView.dataSource = self;
        [tableView registerNib:[UINib nibWithNibName:@"MemberTableViewCell" bundle:nil] forCellReuseIdentifier:MemberTableViewCellID];
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        //下拉刷新
        tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            NSLog(@"---headerWithRefreshingBlock");
            [self.models removeAllObjects];
            
            self.pageIndex = 1;
            [self loadData];
        }];
        //上拉加载
        tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            NSLog(@"---footerWithRefreshingBlock");
            
            self.pageIndex ++;
            [self loadData];
        }];
        
        _tableView = tableView;
    }
    
    return _tableView;
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return MemberListCellH;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MemberTableViewCellID forIndexPath:indexPath];
    if(!cell){
        cell = [[MemberTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MemberTableViewCellID];
    }
    
    [cell.headerImgView sd_setImageWithURL:[NSURL URLWithString:self.models[indexPath.section].picUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    
    cell.namelab.text = self.models[indexPath.section].username;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    LoginModel *model = self.models[indexPath.section];
    UserProfileViewController *VC = [[UserProfileViewController alloc] init];
    VC.memberId = model.pk;
    VC.isMemberList = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark loadData
- (void)loadData {
    if(!self.apiStr || !self.params){
        [self showAlert:@"参数错误"];
        return;
    }
    self.params[@"pageIndex"] = @(self.pageIndex);
    self.params[@"pageSize"] = @(PAGEING_SIZE);
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", kFirstUrl, self.apiStr];
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [DataContrller PostRequestUrl:urlStr Param:self.params requestSuccess:^(NSDictionary *backDic) {
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        NSLog(@"%@-backDic:%@", self.apiStr, backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            if(self.pageIndex == 1){
                [self.models removeAllObjects];
            }
            if(!backDic[@"obj"] || [backDic[@"obj"] isEqual:[NSNull null]]){
                return;
            }
            
            NSDictionary *obj = backDic[@"obj"];
            NSArray *datas = [obj objectForKey:@"customers"];
            for(NSDictionary *data in datas){
                [self.models addObject:[[LoginModel alloc] initWithDictionary:data error:nil]];
            }
            [self.tableView reloadData];
            
        } else {
            [self showAlert:backDic[@"msg"]];
        }
    } requestFail:^(NSString *backErrorStr) {
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@-backErrorStr:%@", self.apiStr, backErrorStr);
        
    }];
}

@end
